package com.zmn.oms.business.impl.perf;

import org.apache.dubbo.config.annotation.Reference;
import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.mcc.common.dto.dept.DeptDRO;
import com.zmn.mcc.dubbo.interfaces.dept.DeptListRemoteService;
import com.zmn.mcc.dubbo.interfaces.staff.StaffListRemoteService;
import com.zmn.oms.business.interfaces.perf.PerfDimensionBService;
import com.zmn.oms.common.constant.DimensionTypeEnum;
import com.zmn.oms.model.entity.perf.PerfDimensionDistribute;
import com.zmn.oms.services.interfaces.perf.PerfDimensionDistributeService;
import com.zmn.base.plat.engine.common.constant.DubboConsts;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.Getter;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

/**
 * PerfDistributeDimensionBServiceImpl
 *
 * @author hankangshuai
 * @since 2019/11/21
 **/
@Service
public class PerfDistributeDimensionBServiceImpl implements
    PerfDimensionBService<PerfDimensionDistribute> {

  private static final String RPC_ERROR_MSG = "远程接口调用失败";

  private static final Integer MAX_STAFF_SIZE = 5000;
  @Resource
  PerfDimensionDistributeService perfDimensionDistributeService;
  @Reference(version = DubboConsts.INTERFACE_VERSION, check = false)
  StaffListRemoteService staffListRemoteService;
  @Reference(version = com.zmn.mcc.dubbo.dto.DubboConsts.INTERFACE_VERSION, check = false)
  DeptListRemoteService deptListRemoteService;
  @Getter
  private DimensionTypeEnum dimensionFor = DimensionTypeEnum.DISTRIBUTE;

  /**
   * 同步纬度
   *
   * @return 更新数量
   */
  @Override
  public Integer syncDimension() {
    // 啄木鸟派单顶级部门
    int zmnDeptID = 2043;
    List<Integer> zmnDeptList = listChildDept(zmnDeptID);
    // 啄木鸟家修匠顶级部门
    int zmnJXJDeptID = 4471;
    List<Integer> zmnJXJDeptList = listChildDept(zmnJXJDeptID);
    if (!zmnJXJDeptList.isEmpty()) {
      zmnDeptList.addAll(zmnJXJDeptList);
    }
    // 言而有信派单顶级部门
    int yeyxDeptId = 4337;
    List<Integer> yeyxDeptList = listChildDept(yeyxDeptId);

    zmnDeptList.add(zmnDeptID);
    zmnDeptList.add(zmnJXJDeptID);
    yeyxDeptList.add(yeyxDeptId);
    List<Integer> allStaffDeptId = Lists.newArrayList(zmnDeptList);
    allStaffDeptId.addAll(yeyxDeptList);

    // 查部门下的员工
    List<PerfDimensionDistribute> distributes = allStaffDeptId.stream().flatMap(deptId -> {
      ResponseDTO<List<VtDTO>> listResponseDTO = staffListRemoteService
          .listStaffVtByDeptIdsAndNumber(Collections.singletonList(deptId), GlobalConsts.YES,
              MAX_STAFF_SIZE);
      if (!listResponseDTO.isSuccess()) {
        throw new RuntimeException("员工接口调用失败");
      }
      return Optional.ofNullable(listResponseDTO.getData()).orElseGet(Collections::emptyList)
          .stream().map(vtDTO -> {
            PerfDimensionDistribute perfDimensionDistribute = new PerfDimensionDistribute();
            perfDimensionDistribute.setDeptId(deptId);
            perfDimensionDistribute.setPlat(
                zmnDeptList.contains(deptId) ? GlobalConsts.PLAT_MARK_ZMN
                    : GlobalConsts.PLAT_MARK_YEYX);
            perfDimensionDistribute.setDistributorId(vtDTO.getValue());
            perfDimensionDistribute.setDistributorName(vtDTO.getText());
            perfDimensionDistribute.setSort(vtDTO.getValue());
            return perfDimensionDistribute;
          });
    }).collect(Collectors.toList());
    return CollectionUtils.isEmpty(distributes) ? 0
        : perfDimensionDistributeService.insertOnDuplicateIgnoreBatch(distributes);
  }

  private List<Integer> listChildDept(int zmnDeptID) {
    ResponseDTO<List<DeptDRO>> listResponseDTO = deptListRemoteService
        .listDeptByParentId(zmnDeptID, GlobalConsts.YES);
    if (!listResponseDTO.isSuccess()) {
      throw new RuntimeException("部门接口调用失败！");
    }
    List<DeptDRO> deptDROS = Optional.ofNullable(listResponseDTO.getData())
        .orElseGet(Collections::emptyList);
    return deptDROS.stream().map(DeptDRO::getDeptId).collect(Collectors.toList());
  }

  /**
   * 获取所有纬度提供者
   *
   * @return 所有纬度提供者
   */
  @Override
  public Supplier<List<PerfDimensionDistribute>> getAllDimensionSupplier() {
    return () -> perfDimensionDistributeService.listAll();
  }
}
